package com.example.springLearning.part14.util;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter
@Slf4j
public class TimeCostFilter implements Filter {

    public TimeCostFilter() {
        System.out.println("construct");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      log.info("开始计算接口耗时");
      long startTime = System.currentTimeMillis();
      chain.doFilter(request, response);
      long endTime = System.currentTimeMillis();
      long time = endTime - startTime;
        System.out.println("执行时间(ms): " + time);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}
